<?php
	class scenic{
		//取得所有景区
		public  function getScenicList(){
			$t=new db_recordset(NULL,"zyw_scenic");
			$sql="select * from zyw_scenic order by pv";
			$t->query($sql,-1);
			$snlist=$t->getRecords();
			return $snlist;
		}
		//根据景区ID取景区信息
		public function getScenicById($sid){
			$t=new db_recordset(NULL,"zyw_scenic");
			$sql="select * from zyw_scenic where sid=$sid";
			$t->query($sql,-1);
			$scenic=$t->getRecords();
			return $scenic[0];
		}
		/*取地区信息*/
		public function getRegionByLevel($level=-1){
			$t=new db_recordset(NULL,"zyw_scenic_region");
			if($level==-1)	$sql="select * from zyw_scenic_region order by rlevel";
			else	$sql="select * from zyw_scenic_region where rlevel=$level";
			$t->query($sql,-1);
			$rl=$t->getRecords();
			return $rl;
		}
		/*增加景区*/
		public function addScenic($scenicParam){
			$t=new db_table(NULL,"zyw_scenic");
			$t->insert();
			$t->name=$scenicParam['name'];
			$t->addr=$scenicParam['addr'];
			$t->level=$scenicParam['level'];
			$t->regiondesc=$scenicParam['regiondesc'];
			$t->weather=$scenicParam['weather'];
			$t->environment=$scenicParam['environment'];
			$t->recommend=$scenicParam['recommend'];
			$t->travelguide=$scenicParam['travelguide'];
			$t->review=$scenicParam['review'];
			$t->recommend=$scenicParam['recommend'];
			$t->state=1;
			$t->pv=0;
			$t->post();
			$sid=$t->sid;
		
			$arealist=array();
			if($scenicParam['district']!=-1)	$arealist[]=$scenicParam['district'];
			if($scenicParam['city']!=-1)	$arealist[]=$scenicParam['city'];
			if($scenicParam['province']!=-1)	$arealist[]=$scenicParam['province'];
			if($scenicParam['area']!=-1)	$arealist[]=$scenicParam['area'];
			if($scenicParam['foreign']!=-1)	$arealist[]=$scenicParam['foreign'];
			$ret=$this->addRegion2Scenic($arealist,$sid);
			return $ret;
		}
		private function addRegion2Scenic($areaArr,$sid){
			$t=new db_table(NULL,"zyw_scenic2region");
			foreach ($areaArr as $area){
				$t->insert();
				$t->sid=$sid;
				$t->rid=$area;
				$t->type='0';//增加景区
				$t->post();
				//$this->addRegionRef($area);
			}
			return true;
		}
		private function addRegionRef($rid){
			$t=new db_table(NULL,"zyw_scenic_region");
			$t->locate($rid,-1);
			if($t->record_count()!=1) return false;
			$t->ref++;
			$t->post();
			return true;
		}
		private function delRegion2Scenic($sid){
			$t=new db_recordset(NULL,"zyw_scenic2region");
			$sql="delete from zyw_scenic2region where sid=$sid";
			$t->exec($sql,-1);
		}
		public function editScenicById($sid,$scenicParam){
			$t=new db_table(NULL,"zyw_scenic");
			$t->locate($sid,-1);
			if($t->record_count()!=1)	return false;
			$t->name=$scenicParam['name'];
			$t->addr=$scenicParam['addr'];
			$t->level=$scenicParam['level'];
			$t->regiondesc=$scenicParam['regiondesc'];
			$t->weather=$scenicParam['weather'];
			$t->environment=$scenicParam['environment'];
			$t->recommend=$scenicParam['recommend'];
			$t->travelguide=$scenicParam['travelguide'];
			$t->review=$scenicParam['review'];
			$t->recommend=$scenicParam['recommend'];
			$t->post();
			
			$arealist=array();
			if($scenicParam['district']!=-1)	$arealist[]=$scenicParam['district'];
			if($scenicParam['city']!=-1)	$arealist[]=$scenicParam['city'];
			if($scenicParam['province']!=-1)	$arealist[]=$scenicParam['province'];
			if($scenicParam['area']!=-1)	$arealist[]=$scenicParam['area'];
			if($scenicParam['foreign']!=-1)	$arealist[]=$scenicParam['foreign'];
			$this->delRegion2Scenic($sid);
			$ret=$this->addRegion2Scenic($arealist,$sid);
			return $ret;
		}
		public function getRegion2Scenic($sid){
			$t=new db_recordset(NULL,"zyw_scenic2region");
			$sql="select a.*,b.rname,b.rlevel from zyw_scenic2region a,zyw_scenic_region b where a.rid=b.rid and sid=$sid";
			$t->query($sql,-1);
			$reg2scenic=$t->getRecords();
			return $reg2scenic;
		}
		public function getGalleryFromScenic($sid){
			$t=new db_recordset(NULL,"zyw_scenic_photo");
			$sql="select * from zyw_scenic_photo where sid=$sid";
			//debug::output(1,$sql);
			$t->query($sql,-1);
			$gallery=$t->getRecords();
			return $gallery;
		}
		public function getTicketListFromScenic($sid){
			$t=new db_recordset(NULL,"zyw_ticket_main");
			$sql="select * from zyw_ticket_main where sid=$sid";
			$t->query($sql,-1);
			$ticketList=$t->getRecords();
			return $ticketList;
		}
		public function addTicket($paramArr){
			$t=new db_table(NULL,"zyw_ticket_main");
			$t->insert();
			$t->sid=$paramArr['sid'];
			$t->type=$paramArr['type'];
			$t->name=$paramArr['name'];
			$t->ptype=$paramArr['ptype'];
			$t->season=$paramArr['season'];
			$t->pricetype=$paramArr['pricetype'];
			$t->recommend=$paramArr['recommend'];
			$t->memo=$paramArr['memo'];
			$t->memodetail=$paramArr['memodetail'];
			$t->price=$paramArr['price'];
			$t->realprice=$paramArr['realprice'];
			$t->state=0;//创建
			$t->create_date=date("Y-m-d");
			$t->update_date=date("Y-m-d");
			$t->post();
			return true;
		}
		public function delTicketById($tid){
			$t=new db_recordset(NULL,"zyw_ticket_main");
			$sql="delete from zyw_ticket_main where tid=$tid";
			$t->exec($sql,-1);
			return true;
		}
		public function getTicketById($tid){
			$t=new db_recordset(NULL,"zyw_ticket_main");
			$sql="select * from zyw_ticket_main where tid=$tid";
			$t->query($sql,-1);
			$ticket=$t->getRecords();
			return $ticket[0];
		}
		public function editTicketById($tid,$paramArr){
			$t=new db_table(NULL,"zyw_ticket_main");
			$t->locate($tid,-1);
			if($t->record_count()!=1)	return false;
			//$t->sid=$paramArr['sid'];
			$t->type=$paramArr['type'];
			$t->name=$paramArr['name'];
			$t->ptype=$paramArr['ptype'];
			$t->season=$paramArr['season'];
			$t->pricetype=$paramArr['pricetype'];
			$t->recommend=$paramArr['recommend'];
			$t->memo=$paramArr['memo'];
			$t->memodetail=$paramArr['memodetail'];
			$t->price=$paramArr['price'];
			$t->realprice=$paramArr['realprice'];
			$t->update_date=date("Y-m-d");
			$t->post();
			return true;
		}
		public function disableTicket($tid){
			$t=new db_table(NULL,"zyw_ticket_main");
			$t->locate($tid,-1);
			if($t->record_count()!=1)	return false;
			$t->state=2;
			$t->post();
			return true;
		}
		public function enableTicket($tid){
			$t=new db_table(NULL,"zyw_ticket_main");
			$t->locate($tid,-1);
			if($t->record_count()!=1)	return false;
			$t->state=1;
			$t->post();
			return true;
		}
		public function getSubject(){
			$t=new db_recordset(NULL,"zyw_scenic_subject");
			$sql="select * from zyw_scenic_subject";
			$t->query($sql,-1);
			$subArr=$t->getRecords();
			return $subArr;
		}
		public function getSenicSubject($sid){
			$t=new db_recordset(NULL,"zyw_scenic_subject");
			$sql="select a.subid from zyw_subject_relation a where sid=$sid and type=0";
			$t->query($sql,-1);
			$scenicSubArr=$t->getRecords();
			return $scenicSubArr;
		}
		public function addSenicSubject($subArr,$sid){
			$t=new db_recordset(NULL,"zyw_subject_relation");
			$sql="delete from zyw_subject_relation where sid=$sid and type=0";
			$t->exec($sql,-1);
			$t0=new db_table(null,"zyw_subject_relation");
			foreach ($subArr as $sub){
				$t0->insert();
				$t0->sid=$sid;
				$t0->subid=$sub;
				$t0->type=0;
				$t0->post();
			}
		}
	}
?>